package com.lnking.lnkingplace.service.impl;

import com.lnking.lnkingplace.mapper.IsolationMedicalPlaceMapper;
import com.lnking.lnkingplace.model.dto.isolation.medical.MedicalPlaceImport;
import com.lnking.lnkingplace.model.entity.IsolationMedicalPlace;
import com.lnking.lnkingplace.model.request.isolation.medicalPlace.MedicalPlaceAddRequest;
import com.lnking.lnkingplace.model.request.isolation.medicalPlace.MedicalPlaceUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationMedicalPlaceService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-21
 */
@Service
public class IsolationMedicalPlaceServiceImpl extends CustomServiceImpl<IsolationMedicalPlaceMapper, IsolationMedicalPlace> implements IIsolationMedicalPlaceService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<MedicalPlaceImport> result =  ImportUtils.importExcel(file, MedicalPlaceImport.class,option);
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getPlaceName())){
                IsolationMedicalPlace info = new IsolationMedicalPlace();
                BeanUtils.copyProperties(r,info);
                info.setCreateTime(LocalDateTime.now());
                info.setOccupancyNumber(Integer.parseInt(r.getOccupancyNumber()));
                info.setDayNewNumber(Integer.parseInt(r.getDayNewNumber()));
                info.setDayMoveNumber(Integer.parseInt(r.getDayMoveNumber()));
                info.setRoomTotal(Integer.parseInt(r.getRoomTotal()));
                info.setUseRoom(Integer.parseInt(r.getUseRoom()));
                info.setUnuseRoom(Integer.parseInt(r.getUnuseRoom()));
                this.baseMapper.insert(info);
            }
        });
    }

    @Override
    public void add(MedicalPlaceAddRequest params) {
        IsolationMedicalPlace info = new IsolationMedicalPlace();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(info);
    }

    @Override
    public void update(MedicalPlaceUpdateRequest params) {
        IsolationMedicalPlace info = new IsolationMedicalPlace();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.updateById(info);
    }
}
